﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace NCS.IConnect.Core.WebApi
{
    /// <summary>
    /// 
    /// </summary>
    public static class MediaTypeFormatterExtension
    {
        /// <summary>
        /// Deep copy this object used for.
        /// </summary>
        /// <param name="formatter"></param>
        /// <param name="settings"><see cref="JsonSerializerSettings"/></param>
        /// <returns></returns>
        public static MediaTypeFormatter Copy(this JsonMediaTypeFormatter formatter, JsonSerializerSettings settings)
        {
            var newFormatter = new JsonMediaTypeFormatter
            {
                Indent = formatter.Indent,
                MaxDepth = formatter.MaxDepth,
                UseDataContractJsonSerializer = formatter.UseDataContractJsonSerializer,
                RequiredMemberSelector = formatter.RequiredMemberSelector,
                SerializerSettings = settings
            };
            return newFormatter;
        }

        /// <summary>
        /// Deep copy this object used for.
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="contractResolver"><see cref="IContractResolver"/> is indicated contract resolver of new object.</param>
        /// <returns></returns>
        public static JsonSerializerSettings Copy(this JsonSerializerSettings settings, IContractResolver contractResolver)
        {
            var newSettings = new JsonSerializerSettings
            {
                //shadow copy
                Binder = settings.Binder,
                CheckAdditionalContent = settings.CheckAdditionalContent,
                ConstructorHandling = settings.ConstructorHandling,
                Context = settings.Context,
                Converters = settings.Converters,
                Culture = settings.Culture,
                DateFormatHandling = settings.DateFormatHandling,
                DateFormatString = settings.DateFormatString,
                DateParseHandling = settings.DateParseHandling,
                DateTimeZoneHandling = settings.DateTimeZoneHandling,
                DefaultValueHandling = settings.DefaultValueHandling,
                Error = settings.Error,
                FloatFormatHandling = settings.FloatFormatHandling,
                FloatParseHandling = settings.FloatParseHandling,
                Formatting = settings.Formatting,
                MaxDepth = settings.MaxDepth,
                MetadataPropertyHandling = settings.MetadataPropertyHandling,
                MissingMemberHandling = settings.MissingMemberHandling,
                NullValueHandling = settings.NullValueHandling,
                ObjectCreationHandling = settings.ObjectCreationHandling,
                PreserveReferencesHandling = settings.PreserveReferencesHandling,
                ReferenceLoopHandling = settings.ReferenceLoopHandling,
                ReferenceResolver = settings.ReferenceResolver,
                StringEscapeHandling = settings.StringEscapeHandling,
                TraceWriter = settings.TraceWriter,
                TypeNameAssemblyFormat = settings.TypeNameAssemblyFormat,
                TypeNameHandling = settings.TypeNameHandling,
                //for override global contract resolver when use response type is DataSet, so must deep copy.
                ContractResolver = contractResolver
            };
            return newSettings;

        }
    }
}
